package com.example.calender.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.calender.BaseActivity;
import com.example.calender.BaseApplication;
import com.example.calender.R;
import com.example.calender.persistence.User;
import com.example.calender.persistence.UserDao;
import com.example.calender.utils.DialogOneEditUtils;
import com.example.calender.utils.MD5Utils;
import com.example.calender.utils.ToastUtil;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.CompletableObserver;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class RegisterActivity extends BaseActivity {

    private static final String TAG = "RegisterActivity";
    TextView phoneTextView,messageTextView,passwordTextView,password2TextView,sureTextView;

    @Override
    protected int setLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void findViews() {
        phoneTextView=(TextView)findViewById(R.id.register_phone_et);
        messageTextView=(TextView)findViewById(R.id.register_message_et);
        passwordTextView=(TextView)findViewById(R.id.register_password_et);
        password2TextView=(TextView)findViewById(R.id.register_password_et_2);
        sureTextView=(TextView)findViewById(R.id.register_sure_tv);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        //注册
        sureTextView.setOnClickListener(new View.OnClickListener() {

            private UserDao userDao;

            @Override
            public void onClick(View v) {
                String registerphone=phoneTextView.getText().toString();
                String registerpwd=passwordTextView.getText().toString();
                String message = messageTextView.getText().toString().trim() + "";
                String registerpwd2=password2TextView.getText().toString();
                if (TextUtils.isEmpty(registerphone)){
                    ToastUtil.showShort("请输入用户名");
                }else if (TextUtils.isEmpty(message)){
                    ToastUtil.showShort("密保不能为空!");
                }else if(TextUtils.isEmpty(registerpwd)){
                    ToastUtil.showShort("请输入密码!");
                }else if(registerpwd.length()<6){
                    ToastUtil.showShort("密码不得小于6位");
                }else if(!registerpwd.equals(registerpwd2)){
                    ToastUtil.showShort("两次密码不一致,请重新输入!");
                }else{
                    userDao = ((BaseApplication) getApplication()).getUserDB().userDao();
                    User user = new User(registerphone, MD5Utils.digest(registerpwd),message);
                    userDao.insert(user).subscribeOn(Schedulers.io())
                            .observeOn(Schedulers.computation())
                            .subscribe(new CompletableObserver() {
                                @Override
                                public void onSubscribe(@NonNull Disposable d) {

                                }

                                @Override
                                public void onComplete() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            DialogOneEditUtils.getInitialize(RegisterActivity.this, new DialogOneEditUtils.DialogClick() {
                                                @Override
                                                public void determineClick() {
                                                    finish();
                                                }

                                                @Override
                                                public void cancelClick() {
                                                    finish();
                                                }
                                            }, "注册成功");
                                        }
                                    });
                                }

                                @Override
                                public void onError(@NonNull Throwable e) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            ToastUtil.showLong("用户名已被注册");
                                        }
                                    });

                                }
                            });

                }
            }
        });

    }

    @Override
    protected void initData() {

    }
}
